package org.jeecg.modules.demo.Sporder.service.impl;

import org.jeecg.modules.demo.Sporder.entity.Sporder;
import org.jeecg.modules.demo.Sporder.entity.Gordergx;
import org.jeecg.modules.demo.Sporder.entity.Gxcp;
import org.jeecg.modules.demo.Sporder.entity.Gxwuliao;
import org.jeecg.modules.demo.Sporder.entity.Gdcp;
import org.jeecg.modules.demo.Sporder.mapper.GordergxMapper;
import org.jeecg.modules.demo.Sporder.mapper.GxcpMapper;
import org.jeecg.modules.demo.Sporder.mapper.GxwuliaoMapper;
import org.jeecg.modules.demo.Sporder.mapper.GdcpMapper;
import org.jeecg.modules.demo.Sporder.mapper.SporderMapper;
import org.jeecg.modules.demo.Sporder.service.ISporderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 生产订单
 * @Author: jeecg-boot
 * @Date:   2021-04-23
 * @Version: V1.0
 */
@Service
public class SporderServiceImpl extends ServiceImpl<SporderMapper, Sporder> implements ISporderService {

	@Autowired
	private SporderMapper sporderMapper;
	@Autowired
	private GordergxMapper gordergxMapper;
	@Autowired
	private GxcpMapper gxcpMapper;
	@Autowired
	private GxwuliaoMapper gxwuliaoMapper;
	@Autowired
	private GdcpMapper gdcpMapper;
	
	@Override
	@Transactional
	public void saveMain(Sporder sporder, List<Gordergx> gordergxList,List<Gxcp> gxcpList,List<Gxwuliao> gxwuliaoList,List<Gdcp> gdcpList) {
		sporderMapper.insert(sporder);
		if(gordergxList!=null && gordergxList.size()>0) {
			for(Gordergx entity:gordergxList) {
				//外键设置
				entity.setKid(sporder.getId());
				gordergxMapper.insert(entity);
			}
		}
		if(gxcpList!=null && gxcpList.size()>0) {
			for(Gxcp entity:gxcpList) {
				//外键设置
				entity.setSporderid(sporder.getId());
				gxcpMapper.insert(entity);
			}
		}
		if(gxwuliaoList!=null && gxwuliaoList.size()>0) {
			for(Gxwuliao entity:gxwuliaoList) {
				//外键设置
				entity.setSporderid(sporder.getId());
				gxwuliaoMapper.insert(entity);
			}
		}
		if(gdcpList!=null && gdcpList.size()>0) {
			for(Gdcp entity:gdcpList) {
				//外键设置
				entity.setSporderid(sporder.getId());
				gdcpMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Sporder sporder,List<Gordergx> gordergxList,List<Gxcp> gxcpList,List<Gxwuliao> gxwuliaoList,List<Gdcp> gdcpList) {
		sporderMapper.updateById(sporder);
		
		//1.先删除子表数据
		gordergxMapper.deleteByMainId(sporder.getId());
		gxcpMapper.deleteByMainId(sporder.getId());
		gxwuliaoMapper.deleteByMainId(sporder.getId());
		gdcpMapper.deleteByMainId(sporder.getId());
		
		//2.子表数据重新插入
		if(gordergxList!=null && gordergxList.size()>0) {
			for(Gordergx entity:gordergxList) {
				//外键设置
				entity.setKid(sporder.getId());
				gordergxMapper.insert(entity);
			}
		}
		if(gxcpList!=null && gxcpList.size()>0) {
			for(Gxcp entity:gxcpList) {
				//外键设置
				entity.setSporderid(sporder.getId());
				gxcpMapper.insert(entity);
			}
		}
		if(gxwuliaoList!=null && gxwuliaoList.size()>0) {
			for(Gxwuliao entity:gxwuliaoList) {
				//外键设置
				entity.setSporderid(sporder.getId());
				gxwuliaoMapper.insert(entity);
			}
		}
		if(gdcpList!=null && gdcpList.size()>0) {
			for(Gdcp entity:gdcpList) {
				//外键设置
				entity.setSporderid(sporder.getId());
				gdcpMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		gordergxMapper.deleteByMainId(id);
		gxcpMapper.deleteByMainId(id);
		gxwuliaoMapper.deleteByMainId(id);
		gdcpMapper.deleteByMainId(id);
		sporderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			gordergxMapper.deleteByMainId(id.toString());
			gxcpMapper.deleteByMainId(id.toString());
			gxwuliaoMapper.deleteByMainId(id.toString());
			gdcpMapper.deleteByMainId(id.toString());
			sporderMapper.deleteById(id);
		}
	}
	
}
